﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


public class EffectManager : MonoBehaviour
{
    public static EffectManager Instace;
    public Dictionary<string, BhvEasyPool> pools = new Dictionary<string, BhvEasyPool>();
    public PoolFactory poolFactory;

    private void Awake()
    {
        Instace = this;
        poolFactory = new PoolFactory(this.transform, "sfx");
    }

    /// <summary>
    /// 移除节点下所有的 粒子效果，并且自动回收进对象池
    /// </summary>
    /// <param name="root"></param>
    public void RemoveAllParticlesAt(Transform root)
    {
        var comps = root.GetComponentsInChildren<BhvParticlesCtrl>();
        for (int i = 0; i < comps.Length; i++)
        {
            PutParticles(comps[i], comps[i].gameObject.name);
        }
    }

    /// <summary>
    /// 移除当前节点下指定名称的粒子，如果没有粒子，则无事发生
    /// </summary>
    /// <param name="root"></param>
    /// <param name="poolName"></param>
    public void RemoveParticlesAt(Transform root, string poolName)
    {
        if (root.childCount <= 0) return;
        var effect = root.Find(poolName);
        if (effect != null)
        {
            // effect.GetComponent<ParticleSystem>()?.Stop();
            // effect.gameObject.SetActive(false);//只是隐藏
            // Destroy(effect);
            this.PutGameObject(effect.gameObject, poolName);
        }
    }

    public BhvParticlesCtrl GetParticlesAt(Transform root, string poolName)
    {
        BhvParticlesCtrl particles = null;
        var effect = root.Find(poolName);
        if (effect == null)
        {
            particles = AddParticlesAt(root, poolName);
        }
        else
        {
            particles = effect.GetComponent<BhvParticlesCtrl>();
        }
        return particles;
    }


    /// <summary>
    /// 创建一个挂载到 Transfrom 节点下 的粒子效果
    /// (注意，同名粒子只能同时存在一个如果粒子已经存在，那直接返回已存在粒子)
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="poolName"></param>
    public BhvParticlesCtrl AddParticlesAt(Transform root, string poolName, Vector3 scale = default)
    {
        BhvParticlesCtrl particles = null;
        var effect = root.Find(poolName);
        if (effect == null)
        {
            particles = GetParticles(poolName);
        }
        else
        {
            particles = effect.GetComponent<BhvParticlesCtrl>();
        }

        if (particles == null)
        {
            return null;
        }

        particles.transform.SetParent(root);
        particles.transform.localScale = (scale == default(Vector3) ? Vector3.one : scale);
        particles.transform.localPosition = Vector3.zero;
        particles.transform.localEulerAngles = Vector3.zero;

        return particles;
    }

    public void PutParticles(BhvParticlesCtrl particle, string name)
    {
        poolFactory.GetPool(name)?.Put(particle.gameObject);
    }

    public BhvParticlesCtrl GetParticles(string name, bool usePool = true, int cacheMax = 0)
    {
        var item = poolFactory.GetObjectFromPool(name, cacheMax);
        BhvParticlesCtrl comp = null;
        if (item)
        {
            item.name = item.name.Replace("(Clone)", "");
            comp = item.GetComponent<BhvParticlesCtrl>();
            if (comp == null)
            {
                comp = item.AddComponent<BhvParticlesCtrl>();
                comp.fromEffectManager = true;
            }
            comp.SetAutoRecycle(usePool);
        }
        return comp;
    }

    public GameObject GetGameObject(string name, bool usePool = true, int cacheMax = 0)
    {
        var item = poolFactory.GetObjectFromPool(name, cacheMax);
        if (item)
        {
            item.name = item.name.Replace("(Clone)", "");
            return item;
        }
        else
        {
            return null;
        }
    }

    public void PutGameObject(GameObject obj, string name)
    {
        poolFactory.GetPool(name)?.Put(obj);
    }


}
